/*
* (c) by Andreas Kreisl
*
* Sys Monitor - Plugin: PhpSysInfo - Physical Memory
* Version: 0.1
*
* License: creative commons BY-NC-SA
*/


// --------------------------------------------------------------------------------
// myPlugin Variables (required!)

myPlugins["phpp_mem"] = new Object();
myPlugins["phpp_mem"]["name"] = getLocalizedString2("mod_sysm_plugin_mem_name");
myPlugins["phpp_mem"]["onlyonce"] = false;
myPlugins["phpp_mem"]["newObject"] = function (NewSettings) { return new plugin_phpp_mem_sensor(NewSettings); }

// --------------------------------------------------------------------------------
// The Sensore Object

function plugin_phpp_mem_sensor(NewSettings) {
	// Required variables
	this.plugin = "phpp_mem";
	this.settings = NewSettings;
	this.edit = plugin_phpp_mem_edit;
	this.ReDraw = plugin_phpp_mem_ReDraw;
	//this.Delete = plugin_phpp_mem_Delete;
	this.Update = plugin_phpp_mem_update;
	this.frame = new Frame();
	this.frame.window = main;
	this.frame.width = mod_sysmonitor_width;


	//Init Settings
	if(!this.settings.color) this.settings.color = "#FFFFFF";
	if(!this.settings.barcolor) this.settings.barcolor = "#008000";
	if(!this.settings.text) this.settings.text = 0;
	if(!this.settings.id)
		for(var i in PHPSysInfo)
			if(i) {
				this.settings.id = i;
				//break;
			}


	//The Sonsore
	this.phys = new class_PercentBar(this.frame,0,0,getLocalizedString2("mod_sysm_plugin_mem_label"));
	this.phys.PercentColor(this.settings.color);
	this.phys.PercentBarColor(this.settings.barcolor);


	// Update timer
	var obj = this;
	plugin_phpp_mem_update.call(obj);


	// Calculating sensore height (required!)
	this.frame.height = this.phys.background.vOffset + this.phys.background.height;
	return true;

// --------------------------------------------------------------------------------
// Helper Functions

	function plugin_phpp_mem_edit() {
		var Drive_Edit_DialogFields = Array();
		var FieldID = -1;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_color");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.color;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_barcolor");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.barcolor;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_mem_pref_text");
		Drive_Edit_DialogFields[FieldID].type = 'popup';
		Drive_Edit_DialogFields[FieldID].option = new Array(getLocalizedString2("mod_sysm_plugin_mem_pref_free"),getLocalizedString2("mod_sysm_plugin_mem_pref_used"),getLocalizedString2("mod_sysm_plugin_mem_pref_total"));
		Drive_Edit_DialogFields[FieldID].optionValue = new Array(0,1,2);
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.text;

		var SList = new Array();
		var SVal = new Array();
		for(var i in PHPSysInfo) {
			SList[SList.length] = PHPSysInfo[i]["Vitals"]["Hostname"];
			SVal[SVal.length] = i;
		}

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_php_host_source");
		Drive_Edit_DialogFields[FieldID].type = 'popup';
		Drive_Edit_DialogFields[FieldID].option = SList;
		Drive_Edit_DialogFields[FieldID].optionValue = SVal;
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.id;


		var Drive_Edit_DialogResults = form(Drive_Edit_DialogFields, getLocalizedString2("mod_sysm_plugin_preftitle") + myPlugins[this.plugin]["name"]);
		if (Drive_Edit_DialogResults != null) {
			this.settings.color = Drive_Edit_DialogResults[0];
			this.phys.PercentColor(this.settings.color);
	
			this.settings.barcolor = Drive_Edit_DialogResults[1];
			this.phys.PercentBarColor(this.settings.barcolor);
	
			this.settings.text = Drive_Edit_DialogResults[2];
	
			this.settings.id = Drive_Edit_DialogResults[3];

			plugin_phpp_mem_update.call(this);
		}
		delete Drive_Edit_DialogFields;
		return true;
	}

	function plugin_phpp_mem_update() {
		if(PHPSysInfo[this.settings.id]) {
			if(this.settings.text==1)
				this.phys.PercentBarValue( 100 * PHPSysInfo[this.settings.id]["Memory"]["App"] / PHPSysInfo[this.settings.id]["Memory"]["Total"] ,bytesToUIString( 1024 * PHPSysInfo[this.settings.id]["Memory"]["App"] ));
			else if(this.settings.text==2)
				this.phys.PercentBarValue( 100 * PHPSysInfo[this.settings.id]["Memory"]["App"] / PHPSysInfo[this.settings.id]["Memory"]["Total"] ,bytesToUIString( 1024 * PHPSysInfo[this.settings.id]["Memory"]["Total"] ));
			else
				this.phys.PercentBarValue( 100 * PHPSysInfo[this.settings.id]["Memory"]["App"] / PHPSysInfo[this.settings.id]["Memory"]["Total"] ,bytesToUIString( 1024 * (PHPSysInfo[this.settings.id]["Memory"]["Total"] - PHPSysInfo[this.settings.id]["Memory"]["App"]) ));

			this.phys.PercentBarPeak( 100 * PHPSysInfo[this.settings.id]["Memory"]["Used"] / PHPSysInfo[this.settings.id]["Memory"]["Total"] );
		} else {
			this.phys.PercentBarValue( 0 ,"---");
		}
		return true;
	}

	function plugin_phpp_mem_ReDraw() {
		this.frame.width = mod_sysmonitor_width;
		this.phys.style = preferences.mod_sysmonitor_bar_style.value;
		this.phys.size = mod_sysmonitor_size;
		this.frame.height = this.phys.background.vOffset + this.phys.background.height;
		return true;
	}

	function plugin_phpp_mem_Delete() {
		return true;
	}

}

// --------------------------------------------------------------------------------
// The END